<?php
include_once THIRD_DIR . "Helper/Vender/xhprof_lib/utils/xhprof_lib.php";
include_once THIRD_DIR . "Helper/Vender/xhprof_lib/utils/xhprof_runs.php";

/**
 * facebook的性能测试工具
 * 
 * Web访问:
 * http://192.168.5.169/tools/xhprof/xhprof_html/?source=[namespace]&run=[runId]
 *
 * @author simon
 */
class Helper_Vender_Xhprof 
{
   
    /**
     * 是否启用
     */
    protected static $enable = false;
    /**
     * 开始监听
     */
    public static function start( $uId )
    {
        if( function_exists( 'xhprof_enable' ) )
        {
            //开发环境启用xhprof的条件
            if( DEBUG == true )
            {
                if( !empty( $_GET['xhprof'] ) )
                {
                    self::$enable = true;
                }
            }

            if( self::$enable == true )
            {
                xhprof_enable( XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY );
            }
        }
    }
    
    /**
     * 停止监听并存储监听记录
     * 
     * @return array(
     *      runId:string        日志运行ID   
     *      namespace:string    名称空间
     * )
     */
    public static function stop()
    {
        if( self::$enable == false )
        {
            return false;
        }
        
        $runId = 0;
        $namespace = 'ProjectY';
        
        if( function_exists( 'xhprof_disable' ) )
        {
            $xhprofData = xhprof_disable();   //返回运行数据
            
            $xhprofRuns = new XHProfRuns_Default();
            $runId = $xhprofRuns->save_run( $xhprofData, $namespace );
        }
        return array( 
            'runId' => $runId,
            'namespace' => $namespace,
        );
    }
}

?>
